package cn.yoaket.file;


        import com.alibaba.fastjson.JSON;
        import net.sf.json.JSONArray;
        import net.sf.json.JSONObject;
        import org.geotools.geometry.jts.JTSFactoryFinder;
        import org.locationtech.jts.geom.*;
        import cn.yoaket.unit.Edge;
        import cn.yoaket.unit.Point;

        import java.io.BufferedReader;
        import java.io.File;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.List;

public class DataPhrase {

    private static StringBuilder jsonReader(String filePath) {

        File file = new File(filePath);
        BufferedReader fis = null;
        StringBuilder jsonStr = new StringBuilder();
        try {
            fis  = new BufferedReader(new FileReader(file));

            String line;
            while((line = fis.readLine()) != null){
                jsonStr.append(line).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return jsonStr;
    }

    public static String toJSON(List<Polygon> polygons){
        List<List<Point>> lists = new ArrayList<>();
        for (Polygon polygon : polygons) {
            Coordinate[] coordinates = polygon.getCoordinates();
            List<Point> list = new ArrayList<>();
            for (Coordinate coordinate : coordinates) {
                list.add(new Point(coordinate.getX(),coordinate.getY()));
            }
            lists.add(list);
        }
        return JSON.toJSON(lists).toString();
    }

    public static List<Edge> roadPhrase(String filePath){
        List<Edge> roads = new ArrayList<>();
        //JSON字符串
        StringBuilder jsonStr = jsonReader(filePath);

        //处理JSON字符串
        JSONObject jsonObj = JSONObject.fromObject(jsonStr.toString());
        JSONArray features = (JSONArray) jsonObj.get("features");

        for (Object feature : features) {
            JSONObject street = (JSONObject) feature;
            JSONObject geometry = (JSONObject) street.get("geometry");
            JSONObject attributes = (JSONObject) street.get("attributes");
            JSONArray paths = (JSONArray) geometry.get("paths");
            //道路宽度
            double width = Double.parseDouble(attributes.get("WIDTH").toString())*360.0/(40030173*0.933)*0.1;
            //对道路进行解析
            for (Object o : paths) {
                JSONArray path = (JSONArray) o;
                Edge edge = new Edge();
                edge.setWidth(width);
                for (Object value : path) {
                    double lon = Double.parseDouble(((JSONArray) value).get(0).toString());
                    double lat = Double.parseDouble(((JSONArray) value).get(1).toString());
                    edge.add(lon, lat);
                }
                roads.add(edge);
            }
        }
        return roads;
    }

    public static List<Polygon> roadPhrase(List<Edge> edges){
        List<Polygon> roads = new ArrayList<>();
        for (Edge edge : edges) {
            roads.add(edge.toPolygon());
        }

        return roads;
    }

    public static List<Polygon> buildingPhrase(String filePath){
        GeometryFactory factory = JTSFactoryFinder.getGeometryFactory();
        List<Polygon> buildings = new ArrayList<>();

        //JSON字符串
        StringBuilder jsonStr = jsonReader(filePath);

        //处理JSON字符串
        JSONObject jsonObj = JSONObject.fromObject(jsonStr.toString());
        JSONArray features = (JSONArray) jsonObj.get("features");

        for (Object feature : features) {
            JSONObject street = (JSONObject) feature;
            JSONObject geometry = (JSONObject) street.get("geometry");
            JSONObject attributes = (JSONObject) street.get("attributes");
            JSONArray rings = (JSONArray) geometry.get("rings");
            //道路宽度
            int floor = Integer.parseInt(attributes.get("Floor").toString());
            //对道路进行解析
            for (Object r : rings) {
                JSONArray ring = (JSONArray) r;
                Coordinate[] building = new Coordinate[ring.size()];
                for(int i = 0;i < ring.size();++i){
                    double lon = Double.parseDouble(((JSONArray) ring.get(i)).get(0).toString());
                    double lat = Double.parseDouble(((JSONArray) ring.get(i)).get(1).toString());
                    building[i] = new Coordinate(lon,lat);
                }
                Polygon polygon = factory.createPolygon(building);
                polygon.setUserData(floor);
                buildings.add(polygon);
            }
        }
        return buildings;
    }
}
